<script lang="ts" setup>
import { deletePatientAPI, getPatientListAPI } from '@/apis/patient'
import type { PatientList } from '@/types/patient'
import { onReady, onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])

const patientList = ref<PatientList[]>([])

onShow(async () => {
  const res = await getPatientListAPI()
  patientList.value = res.data
  isActive.value = res.data[0].id
})
// 删除患者信息
const handleDelete = async (id: string, index: number) => {
  patientList.value = patientList.value.filter((item) => item.id !== id)
  await deletePatientAPI(id)
  uni.showToast({ title: '删除成功', icon: 'success' })
}
const props = defineProps<{
  type: 'consult'
}>()
onReady(() => {
  if (props.type) {
    uni.setNavigationBarTitle({ title: '选择患者' })
  }
})
const isActive = ref()
const handleNext = () => {
  const res = patientList.value.some((item) => item.id === isActive.value)
  console.log('res', res)
  if (res) {
    uni.navigateTo({
      url: `/subpkg_consult/payment/payment?patientId=${isActive.value}`,
    })
  } else {
    uni.showToast({ title: '请选择患者', icon: 'fail' })
  }
}
</script>

<template>
  <scroll-view>
    <!-- {{ patientList }}
    {{ isActive }} -->
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>
      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-for="(item, index) in patientList"
          :key="item.id"
          @click="handleDelete(item.id, index)"
        >
          <view
            class="archive-card"
            :class="{ active: isActive === item.id }"
            @click="isActive = item.id"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">
                {{
                  item.idCard.replace(
                    /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9Xx])$/,
                    '$1********$6'
                  )
                }}
              </text>
              <text class="default" v-if="item.defaultFlag">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg/pages/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="true" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg/pages/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
    <view class="next-step">
      <button v-if="props.type" class="add-btn" @click="handleNext">下一步</button>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './index.scss';
</style>
